Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a {@link PagerAdapter} to generate the pages that the view shows.
ViewPager is most often used in conjunction with {@link android.app.Fragment}, which is a convenient way to supply and manage the lifecycle of each page. There are standard adapters implemented for using fragments with the ViewPager, which cover the most common use cases. These are {@link android.support.v4.app.FragmentPagerAdapter} and {@link android.support.v4.app.FragmentStatePagerAdapter}; each of these classes have simple code showing how to build a full user interface with them.
Views which are annotated with the {@link DecorView} annotation are treated as part of the view pagers ‘decor’. Each decor view’s position can be controlled via its {@code android:layout_gravity} attribute. For example:
if (position < -1) { // [-Infinity,-1) // This page is way off-screen to the left. view.setAlpha(0);
} elseif (position <= 0) { // [-1,0] // Use the default slide transition when moving to the left page view.setAlpha(1 + position); view.setTranslationX(0);
/** * A PageTransformer is invoked whenever a visible/attached page is scrolled. * This offers an opportunity for the application to apply a custom transformation * to the page views using animation properties. * * <p>As property animation is only supported as of Android 3.0 and forward, * setting a PageTransformer on a ViewPager on earlier platform versions will * be ignored.</p> */ publicinterfacePageTransformer{ /** * Apply a property transformation to the given page. * * @param page Apply the transformation to this page * @param position Position of page relative to the current front-and-center * position of the pager. 0 is front and center. 1 is one full * page position to the right, and -1 is one page position to the left. */ publicvoidtransformPage(View page, float position); }
/** * Callback interface for responding to changing state of the selected page. */ publicinterfaceOnPageChangeListener{
/** * This method will be invoked when the current page is scrolled, either as part * of a programmatically initiated smooth scroll or a user initiated touch scroll. * * @param position Position index of the first page currently being displayed. * Page position+1 will be visible if positionOffset is nonzero. * @param positionOffset Value from [0, 1) indicating the offset from the page at position. * @param positionOffsetPixels Value in pixels indicating the offset from position. */ publicvoidonPageScrolled(int position, float positionOffset, int positionOffsetPixels);
/** * This method will be invoked when a new page becomes selected. Animation is not * necessarily complete. * * @param position Position index of the new selected page. */ publicvoidonPageSelected(int position);
/** * Called when the scroll state changes. Useful for discovering when the user * begins dragging, when the pager is automatically settling to the current page, * or when it is fully stopped/idle. * * @param state The new scroll state. * @see ViewPager#SCROLL_STATE_IDLE * @see ViewPager#SCROLL_STATE_DRAGGING * @see ViewPager#SCROLL_STATE_SETTLING */ publicvoidonPageScrollStateChanged(int state); }